﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Business;
using NHibernate;
using ZLCMS.Data.Model;
using NHibernate.Criterion;
using ZLCMS.Business.DwzFrame;
using ZLCMS.Business.Util;
using ZLCMS.Business.TranferParam;
using Newtonsoft.Json;
using System.Text;
using DotNet.Common.TransferParam;

namespace ZLCMS.Web.SystemManage.Basics.Param.Memory
{
    public partial class View : System.Web.UI.Page
    {
        protected string _groupcode;

        protected ParamGroup _gourp;

        protected void Page_Load(object sender, EventArgs e)
        {
            //初始化全局变量值
            _groupcode = Request["groupcode"] == null ? "" : Request["groupcode"];

            _gourp = ParamKit.GetInstance().FindGroup(_groupcode);

            if (_gourp == null)
            {
                return;
            }

            LoadParamGroup();

            BindList();
        }

        /// <summary>
        /// 填充网格数据
        /// </summary>
        protected void BindList()
        {
            grid.DataSource = ParamKit.GetInstance().FindGroup(_groupcode).ItemList;
            grid.DataBind();
        }

        protected void LoadParamGroup()
        {
            rp_expname.DataSource = _gourp.GroupExp;
            rp_expname.DataBind();
        }

        protected string GetExpParamItemHtml(object list)
        {
            //参数组无扩展
            if (_gourp.GroupExp == null)
            {
                return null;
            }

            bool isMatch;
            String htmlMoudle = "<td>{0}</td>";
            StringBuilder htmlStr = new StringBuilder();

            List<ParamItemExp> expItemList = (List<ParamItemExp>)list;

            //由于JSON没有顺序，这里需要通过比较找到对应的扩展
            foreach (ParamGroupExp groupExp in _gourp.GroupExp)
            {
                isMatch = false;

                if (expItemList != null)
                {
                    foreach (ParamItemExp itemExp in expItemList)
                    {
                        if (itemExp.Code == groupExp.Code)
                        {
                            isMatch = true;

                            if (groupExp.Type == ParamGroupExpType.String || groupExp.Type == ParamGroupExpType.Integer || groupExp.Type == ParamGroupExpType.Float)
                            {
                                htmlStr.AppendFormat(htmlMoudle, itemExp.Value);
                            }
                            else if (groupExp.Type == ParamGroupExpType.ParamCombo || groupExp.Type == ParamGroupExpType.ParamList)
                            {
                                //该参数组扩展有关联的参数组代码，需要翻译
                                if (groupExp.Relation != null)
                                {
                                    htmlStr.AppendFormat(htmlMoudle, ParamKit.GetValue(groupExp.Relation, itemExp.Value, ""));
                                }
                                else
                                {
                                    htmlStr.AppendFormat(htmlMoudle, itemExp.Value);
                                }
                            }
                            else
                            {
                                htmlStr.AppendFormat(htmlMoudle, itemExp.Value);
                            }
                            break;
                        }
                    }
                }
                if (!isMatch)
                {
                    htmlStr.AppendFormat(htmlMoudle, "&nbsp;");
                }
            }

            return htmlStr.ToString();
        }
    }
}
